package com.epalmpay.controller.apiweixin;


import com.epalmpay.commom.BaseController;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.entity.TradeOffline;
import com.epalmpay.enumdef.BizEnum;
import com.epalmpay.payplat.IPayAPI;
import com.epalmpay.payplat.result.PayCrcodeCallback;
import com.epalmpay.service.activity.IRedPackageUserService;
import com.epalmpay.service.mall.IMallCategoryService;
import com.epalmpay.service.management.ITemplateMailSendService;
import com.epalmpay.service.member.IMemberService;
import com.epalmpay.service.weixin.ITokenService;
import com.epalmpay.service.weixin.IWalletService;
import com.epalmpay.service.weixin.IWxApiService;
import com.epalmpay.service.weixin.IWxApiWalletService;
import com.epalmpay.util.*;
import com.epalmpay.util.payplat.MapUtil;
import com.epalmpay.util.payplat.SecurityUtil;

import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.HashMap;
import java.util.Map;


/**
 * 
* 类名称：WxApiFindShopController.java
* 类描述： 钱包首页接口
* @author wzq
* 创建时间：2017年7月4日
 */
@Controller
@RequestMapping(value="/api/wallet")
public class WxApiWalletController extends BaseController{

	/**
	 * 
	 */
	
	@Resource
	private IWxApiWalletService wxApiWalletServiceService;
	
	@Resource
	private IWalletService walletServiceService;
	
	@Resource
	private IMemberService memberService;
	@Resource
	private IRedPackageUserService redPackageUserService;
	@Resource
	private IMallCategoryService mallCategoryService;
	@Resource
	private IWxApiService wxApiService;
	@Value("${WX_Key}")
    private String wXKey;
	
	@Autowired
	private ITokenService tokenService;
	
	 @Autowired
	 @Lazy
	 private ITemplateMailSendService templateMailSendService;
	/**
	 * 喜欢商户列表
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/findyourlikeshop", method = RequestMethod.POST)
	@ResponseBody
	public Response findYourLikeShop( @RequestParam(required = true) String memberId,
							 @RequestParam(required = false) String user_longitude,
							 @RequestParam(required = false) String user_latitude,
							 @RequestParam(required = true) Integer pageNo,
							 @RequestParam(required = true) Integer pageSize) throws Exception {
		logBefore(logger, "喜欢商户列表");
		Map<String, Object> map = null;
		try {
			map = wxApiWalletServiceService.getYourLikeShopList(memberId,user_longitude,user_latitude,pageNo,pageSize);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

	/**
	 * 活动门店
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/activityshop", method = RequestMethod.POST)
	@ResponseBody
	public Response activityShop( @RequestParam(required = true) String activityId,
									  @RequestParam(required = false) String user_longitude,
									  @RequestParam(required = false) String user_latitude) throws Exception {
		logBefore(logger, "活动门店");
		Map<String, Object> map = null;

		try {
			map = wxApiWalletServiceService.getActivityShopList(activityId,user_longitude,user_latitude);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

	/**
	 * 活动列表
	 * @param groupId
	 * @return
	 */
	@RequestMapping(value = "/activitylist", method = RequestMethod.POST)
	@ResponseBody
	public Response activityList(@RequestParam(required =true) String groupId){
		Map<String, Object> map = null;

		try {
			map = wxApiService.getActivityList(groupId);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
		
	}
	
	/**
	 * 活动详情
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/activitydetail", method = RequestMethod.POST)
	@ResponseBody
	public Response activityDetail(@RequestParam(required = true) String activityId) throws Exception {
		logBefore(logger, "活动列表详情");
		Map<String, Object> map = null;
		try {
			map = wxApiService.activityDetail(activityId);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

	
	


	/**
	 * 查询优惠券列表(免费领取的优惠券列表)
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/couponcangetlist", method = RequestMethod.POST)
	@ResponseBody
	public Response couponCanGetList(@RequestParam(required = true) String groupId,@RequestParam(required = true) String openId,
							 @RequestParam(required = true) String memberId) throws Exception {
		logBefore(logger, "查询优惠券列表");
		Map<String, Object> map = null;

		try {
			map = wxApiService.getCouponCanGetList(groupId,openId,memberId);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}
	

	/**
	 * 领取优惠卷详情
	 *
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/couponcangetdetail", method = RequestMethod.POST)
	@ResponseBody
	public Response couponCanGetDetail(@RequestParam(required = true) String memberId,
							 @RequestParam(required = true) String couponId) throws Exception {
		logBefore(logger, "领取优惠卷详情");
		Map<String, Object> map = null;
		try {
			map = wxApiService.couponCanGetDetail(memberId,couponId);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}
	
	/**
	 * 领取优惠卷
	 *
	 * @return
	 * @throws Exception			
	 */
	@RequestMapping(value = "/couponget", method = RequestMethod.POST)
	@ResponseBody
	public Response couponGet(@RequestParam(required = true) String memberId,
								   @RequestParam(required = true) String couponId) throws Exception {
		logBefore(logger, "领取优惠卷");

		try {
			int result = wxApiService.insertCouponUser(memberId,couponId);
			if(result == -1)
				throw new ApplicationException(Rescode.FAIL,"优惠券已经结束!");
			if(result == -2)
				throw new ApplicationException(Rescode.FAIL,"优惠券已经领取完成!");
			if(result == -3)
				throw new ApplicationException(Rescode.FAIL,"优惠券已经过期!");
			if(result == -4)
				throw new ApplicationException(Rescode.FAIL,"过了有效结束时间!");
			if(result == -5)
				throw new ApplicationException(Rescode.FAIL,"超过最大领取张数!");

			return Response.getSuccess(null);
		} catch (ApplicationException ae) {
			logger.error(ae);
				if(ae.errMsg.equals("超过最大领取张数!")){
					return Response.getSuccess(-2,ae.errMsg);
				}else{
					return Response.getError(ae.errMsg);
				}
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}
	



	
	
	//已领取红包列表
	@RequestMapping(value = "/redpackagehavegottenlist", method = RequestMethod.POST)
	@ResponseBody
	public Response redpackList(@RequestParam(required = true) String groupId,@RequestParam(required = true) String openId,
							 @RequestParam(required = true) String memberId) throws Exception {
		logBefore(logger, "查询优惠券列表");
		Map<String, Object> map = null;

		try {
			map = wxApiService.getRedpackList(groupId,openId,memberId);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}
	
	
	/**
	 * 红包可领取列表
	 * @param groupId
	 * @param openId
	 * @param memberId
	 * @return
	 * @throws Exception
	 */			
	@RequestMapping(value = "/redpackagecangetlist", method = RequestMethod.POST)
	@ResponseBody
	public Response redpackageCanGetList(@RequestParam(required = true) String groupId,@RequestParam(required = true) String openId,
							 @RequestParam(required = true) String memberId) throws Exception {
		logBefore(logger, "查询优惠券列表");
		Map<String, Object> map = null;
		try {
			map = wxApiService.getRedpackCanGetList(groupId,openId,memberId);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}
	
	
	/**
	 * 注册钱包
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/loginWallet", method = RequestMethod.POST)
	@ResponseBody
	public Response loginWallet(String memberId,String phone,String loginPassword,String payPassword,String code,String type
			,String certName,String certNum,String birthday,String sex) throws Exception {
		logBefore(logger, "注册钱包");
//		Map<String, Object> map = new HashMap<String, Object>();
		try {
			//首先校验短验证码是否正确
			HashMap<String, String> resultMap = new HashMap<String, String>();
			resultMap.put("会员编号", memberId);
			resultMap.put("手机号码", phone);
			resultMap.put("短信验证码", code);
			resultMap.put("短信类型", type);
			resultMap.put("性别", sex);
			resultMap.put("支付密码", payPassword);
			String nullParam=Response.checkNull(resultMap);
			if (null != nullParam) {
				return Response.getError(nullParam+"不能为空");
			} 
		   	Subject currentUser = SecurityUtils.getSubject();  
			Session session = currentUser.getSession();
			//String codemessage=(String)Const.CacheMapObject.get(Const.SESSION_PHONE_CODE+phone+type);
			RedisUtil redisUtil = new RedisUtil();
			Jedis jedis = redisUtil.getJedis();
			String codemessage = jedis.get(Const.SESSION_PHONE_CODE+phone+type);
			if(Tools.isEmpty(codemessage)||!codemessage.equals(code)){ 
				return Response.getError("验证码输入错误");
			}
			//缓解点击频率
			String repeat=(String) session.getAttribute(phone);
			if(Tools.isEmpty(repeat)){	
				session.setAttribute(memberId, Tools.getRandomNumABC(5)); 
				Map<String, Object> map=wxApiService.updateloginWallet(memberId, phone,loginPassword,payPassword,certName,certNum,ROOT_HOME_URL,birthday,sex);
				if(map!=null){
					//session.setTimeout(3*1000);
					//Const.CacheMapObject.remove(Const.SESSION_PHONE_CODE+phone+type);
					//删除相应的数据
					jedis.del(Const.SESSION_PHONE_CODE+phone+type);
					jedis.quit();
					 try {
				            templateMailSendService.sendMemberTemplate(Long.valueOf(memberId), BizEnum.WebTemplateType.template1.getType(), ROOT_HOME_URL, null);
				        } catch (Exception e) {
				            System.out.println("模板消息发送失败" + e);
				            e.printStackTrace();
				        }
					return Response.getSuccess(map); 
				}else{
					return Response.getError("服务器异常");
				}
			}else{
				return Response.getError("操作太快,休息一会再试");
			}
		}
		catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}
	
	
	
	/**获取钱包二维码信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getpaycrcode", method = {RequestMethod.GET,RequestMethod.POST} )
	@ResponseBody
	public Response getpaycrcode(HttpServletRequest request, HttpServletResponse response,String memberId) throws Exception{
		logBefore(logger, "用户钱包二维码获取");
		
		HashMap<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("会员编号", memberId);
		String nullParam=Response.checkNull(resultMap);
		if (null != nullParam) {
			return Response.getError(nullParam+"不能为空");
		}
		try {
			//默认一个临时文件
			String temporary =Tools.getRandomNum(8)+".png";
    		String logoPath =request.getSession().getServletContext().getRealPath("/") +"logo.jpg";
    		//回调url 待开发
    		Map<String, Object> map=wxApiService.queryPaycrcode(memberId,temporary,logoPath,HOME_URL+"/api/wallet/crcodeCallbackPay"); 
			if(map == null){
				return Response.getError("二维码生成失败");
			}else{
				return Response.getSuccess(map);
			}
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}
	
	    
	/**获取结果通知回调
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/crcodeCallbackPay", method = {RequestMethod.GET,RequestMethod.POST} )
	@ResponseBody
	public Response crcodeCallbackPay(PayCrcodeCallback payCrcodeCallback) throws Exception{
		logBefore(logger, "获取结果通知回调"+payCrcodeCallback.getOrgOrderNum());   
		PageData pd = new PageData();
	    pd = this.getPageData();
        if(IPayAPI.checkPayCrcodeNotify(payCrcodeCallback)){
    		//以下具体的逻辑处理
    		//payCrcodeCallback的里面数据都是已经进行解密后的数据可直接使用
        	logBefore(logger, "解密的订单号"+payCrcodeCallback.getOrgOrderNum());   
	    	String qrCode = payCrcodeCallback.getQrCode();
	    	wxApiService.updateTransOrgOrderNum(qrCode, payCrcodeCallback.getOrgOrderNum(),ROOT_HOME_URL);
	    	}
		return null;
	}
	
	
	/**钱包交易成功详情
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/codeSuccessDetails", method = {RequestMethod.GET,RequestMethod.POST} )
	@ResponseBody
	public Response codeSuccessDetails(String orderNum) throws Exception{
		logBefore(logger, "钱包交易成功详情");   
		HashMap<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("订单编号", orderNum);
		String nullParam=Response.checkNull(resultMap);
		if (null != nullParam) {
			return Response.getError(nullParam+"不能为空");
		}
		try {
			Map<String, Object> map=wxApiService.queryCodeSuccessDetails(orderNum); 
			if(map==null){
				return Response.getError("未查询到记录");
			}else{
				return Response.getSuccess(map);
			}
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
		
	}
	
	
	
	/**获取钱包支付结果
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/payPolling", method = {RequestMethod.GET,RequestMethod.POST} )
	@ResponseBody
	public Response payPolling(String memberId,String marked) throws Exception{
		logBefore(logger, "用户钱包二维码获取结果通知");   
		HashMap<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("会员编号", memberId);
		resultMap.put("支付编号", marked);
		String nullParam=Response.checkNull(resultMap);
		if (null != nullParam) {
			return Response.getError(nullParam+"不能为空");
		}
		try {
			Map<String, Object> map=wxApiService.queryPayPolling(memberId, marked); 
			if(map==null){
				return Response.getError("未查询到记录");
			}else{
				return Response.getSuccess(map);
			}
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
		
	}
	
	
	
	/**根据memberId 生成服务商品订单
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/walletpaydisplay", method = RequestMethod.POST)
	@ResponseBody
	public Response walletpaydisplay(Long memberId,String goodsId,String goodsPrice,String amount,
			 Integer bonus, String couponSns) throws Exception{
		logBefore(logger, "根据memberId 生成服务商品订单");
		HashMap<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("会员编号", memberId+"");
		resultMap.put("商品编号", goodsId);
		resultMap.put("商品价格", goodsPrice);
		resultMap.put("商品数量", amount);
	//	resultMap.put("优惠券编码", couponSns);
		
		String nullParam=Response.checkNull(resultMap);
		if (null != nullParam) {
			return Response.getError(nullParam+"不能为空");
		}
		try {
			Map<String, Object> map=wxApiWalletServiceService.updateWalletpaydisplay(memberId,goodsId,goodsPrice,amount,bonus,couponSns);
			if(map.get("ErrorMsg")!=null&&map.get("ErrorMsg")!=""){
				return Response.getError(map.get("ErrorMsg").toString());
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
		
	}
	
	    
	/**支付提交
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/walletpaySubmit", method = RequestMethod.POST)
	@ResponseBody
	public Response walletpaySubmit(String memberId,String orderId,String payType,String payPwd) throws Exception{
		logBefore(logger, "根据memberId生成线上订单");
		HashMap<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("会员编号", memberId);
		resultMap.put("订单编号", orderId);
		resultMap.put("支付方式", payType+"");
		resultMap.put("支付密码", payPwd);
		String nullParam=Response.checkNull(resultMap);
		if (null != nullParam) {
			return Response.getError(nullParam+"不能为空");
		}
		try {
			Map<String, Object> map=wxApiWalletServiceService.updateWalletpaySubmit(memberId,orderId,payType,payPwd,ROOT_HOME_URL);
			try {
				TradeOffline tradeOffline=(TradeOffline) map.get("tradeOffline");
				if(tradeOffline!=null){
					memberService.insertMemberTradeDataAsyn(tradeOffline.getPayerId(),tradeOffline.getRealAmount(),tradeOffline.getBonusValue());
					System.out.print("::::::::::::::::::::::::::服务商品首次购买");
					//首次消费
					memberService.updateMemberFirstTradeSend(tradeOffline.getId(),tradeOffline.getPayerId(),tradeOffline.getGroupId(),tradeOffline.getRealAmount());
				}
			} catch (Exception e) {
				map.remove("tradeOffline");
				return Response.getSuccess(map);
			}
			map.remove("tradeOffline");
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
		
	}


	/**
	 * 根据服务订单号查详情
	 * @param groupId
	 * @param openId
	 * @param memberId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getServiceOrderDetailByOrderId", method = RequestMethod.POST)
	@ResponseBody
	public Response getServiceOrderDetailByOrderId(@RequestParam(required = true) String groupId,
										 @RequestParam(required = true) String openId,
										 @RequestParam(required = true) String memberId,
										@RequestParam(required = true) String orderId) throws Exception {
		logBefore(logger, "根据服务订单号查详情");
		Map<String, Object> map = null;
		try {
			map = wxApiWalletServiceService.getServiceOrderDetailByOrderId(groupId,openId,memberId,orderId);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}


	/**
	 * 获取会员积分信息
	 * @param groupId
	 * @param openId
	 * @param memberId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getBonusInfo", method = RequestMethod.POST)
	@ResponseBody
	public Response getBonusInfo(@RequestParam(required = true) String groupId,
												   @RequestParam(required = true) String openId,
												   @RequestParam(required = true) String memberId) throws Exception {
		logBefore(logger, "获取会员积分信息");
		Map<String, Object> map = null;
		try {
			map = wxApiWalletServiceService.getBonusInfo(groupId,openId,memberId);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

	/**
	 * 获取会员积分明细
	 * @param groupId
	 * @param openId
	 * @param memberId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getBonusInfoDetail", method = RequestMethod.POST)
	@ResponseBody
	public Response getBonusInfoDetail(@RequestParam(required = true) String groupId,
								 @RequestParam(required = true) String openId,
								 @RequestParam(required = true) String memberId,
								 @RequestParam(required = false) String startDate,
									   @RequestParam(required = false) String endDate,
									   @RequestParam(required = true) Integer pageNo,
									   @RequestParam(required = true) Integer pageSize
									   ) throws Exception {
		logBefore(logger, "获取会员积分明细");
		Map<String, Object> map = null;
		try {
			map = wxApiWalletServiceService.getBonusInfoDetail(groupId,openId,memberId,startDate,endDate,pageNo,pageSize);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

	/**
	 * 获取会员积分明细
	 * @param groupId
	 * @param openId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getBonusRule", method = RequestMethod.POST)
	@ResponseBody
	public Response getBonusInfoDetail(@RequestParam(required = true) String groupId,
									   @RequestParam(required = true) String openId
	) throws Exception {
		logBefore(logger, "获取会员积分规则");
		Map<String, Object> map = null;
		try {
			map = wxApiWalletServiceService.getBonusRule(groupId,openId);
			if(map == null || map.size()==0){
				throw new ApplicationException(Rescode.FAIL,"未查询到记录");
			}
			return Response.getSuccess(map);
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
	}

	
	
	/**增加(减少)冻结金额
	 * @return   
	 * @throws Exception
	 */
	@RequestMapping(value = "/changeLockedAmount", method = RequestMethod.POST)
	@ResponseBody
	public Response changeLockedAmount(String id,String type,String tranAmt,String isWithdraw,Integer day, String orderId, String sign) throws Exception{
		logBefore(logger, "增加(减少)冻结金额");
		HashMap<String, String> resultMap = new HashMap<String, String>();
		resultMap.put("编号", id);
		resultMap.put("用户类型", type);
		resultMap.put("交易金额", tranAmt);
		resultMap.put("提现类型", isWithdraw);
		resultMap.put("锁定期限", day+"");
		resultMap.put("订单号", orderId);
		String nullParam=Response.checkNull(resultMap);
		if (null != nullParam) {
			return Response.getError(nullParam+"不能为空");
		}
		
		
		Map<String, String> params = new HashMap<String, String>();
		params.put("id", id);
		params.put("type", type);
		params.put("tranAmt", tranAmt);
		params.put("isWithdraw", isWithdraw);
		params.put("day", day+"");
		params.put("orderId", orderId);
		params = MapUtil.order(params);
		String signSrc = MapUtil.mapJoin(params, false, false);
		if(!SecurityUtil.MD5Hex(signSrc).equals(sign)){
			return Response.getError("签名错误");
		}
		
		try {
			Response response=walletServiceService.updateLockedAmount(id,type,tranAmt, isWithdraw,day,orderId);
			return response;
		} catch (ApplicationException ae) {
			logger.error(ae);
			return Response.getError(ae.errMsg);
		}
		catch (Exception e) {
			logger.error(e);
			return Response.getError("服务器异常");
		}
		
	}


}
